package AD;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class  ConexionBD {

    private static final String DRIVER = "com.mysql.jdbc.Driver";
    private static final String URL = "jdbc:mysql://localhost/BLUECHERRYDB";
    private static final String USUARIO = "root";
    private static final String PASS = "root";

    /**
     * registra el driver de MySQL
     * @throws Exception
     */
    public static void registraDriver() throws Exception {
        try {
            Class.forName(DRIVER);
        } catch (Exception ex) {
            throw new Exception("No se ha registrado el driver\n" + ex.getMessage());
        }
    }
    

    /**
     * Establece una conexion a la base de datos
     * @return la conexion
     * @throws Exception
     */
    public static Connection abreConnexio() throws Exception {
        Connection con = null;
        try {
            con = DriverManager.getConnection(URL, USUARIO, PASS);
            return con;
        } catch (SQLException e) {
            throw new Exception("No se ha establecido la connexion\n"
                    + e.getMessage() + "(" + e.getErrorCode() + ")");
        }
    }

    /**
     * cierra la conexion a la BD
     * @param conn
     * @param rs
     * @throws SException
     */
    public static void cierraRecursos(Connection conn, ResultSet rs) throws Exception {
        
        if (conn != null) {
            try {
				conn.close();
			} catch (SQLException e) {
				 throw new Exception("No se ha podido cerrar  la connexion\n"
		                    + e.getMessage() + "(" + e.getErrorCode() + ")");
			}
        }
        if (rs != null) {
            try {
				rs.close();
			} catch (SQLException e) {
				 throw new Exception("No se ha podido cerrar el resultSet\n"
		                    + e.getMessage() + "(" + e.getErrorCode() + ")");
			}
        }
    }

}
